import { Injectable } from '@angular/core';

import 'rxjs/add/observable/of';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/delay';
import {Headers, Http} from '@angular/http';
import {environment} from '../../environments/environment';
import {XAuth} from '../shared/auth/x-auth';
import {User} from '../shared/auth/user';

@Injectable()
export class AuthService {
    private host: string = environment.host;
    private _xAuth: XAuth;
    isLoggedIn = false;
    redirectUrl: string;
    constructor(private http: Http ) { }
    // store the URL so we can redirect after logging in


    get xAuth(): XAuth {
        return this._xAuth;
    }

    set xAuth(value: XAuth) {
        this._xAuth = value;
    }

    login(user: User): Promise<XAuth> {

        const url = `${ this.host }/api/auth/login`;
        return this.http.post(url, JSON.stringify(user))
            .toPromise().then(response => response.json() as XAuth)
            .then(response => this.xAuth = response);
     // return Observable.of(true).delay(1000).do(val => this.isLoggedIn = true);
     }

    logout(): void {
      this.isLoggedIn = false;
    }
}
